package com.tssf.car_manage.service.impl;

import com.tssf.car_manage.entity.Admin;
import com.tssf.car_manage.mapper.AdminMapper;
import com.tssf.car_manage.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    private final AdminMapper adminMapper;

    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Override
    public List<Admin> getAllAdmins() {
        return adminMapper.lists(); // 调用正确的Mapper方法
    }

    @Override
    public List<Admin> getAdminById(int id) {
        return adminMapper.getById(id);
    }

    @Override
    public int addAdmin(Admin admin) {
        return adminMapper.save(admin);
    }

    @Override
    public int updateAdmin(Admin admin) {
        return adminMapper.update(admin);
    }

    @Override
    public int deleteAdmin(int id) {
        return adminMapper.delete(id);
    }

    @Override
    public List<Admin> searchAdmins(String key) {
        if (key == null || key.trim().isEmpty()) {
            return getAllAdmins(); // 如果关键词为空，返回所有管理员
        }
        return adminMapper.searchAdmins(key.toLowerCase()); // 转为小写进行不区分大小写搜索
    }

    @Override
    public List<Admin> getAdminByUsername(String username) {
        return adminMapper.getAdminByUsername(username);
    }
}




